//
//  StarView.m
//  IFreeAppDemo
//
//  Created by qianfeng on 15/11/5.
//  Copyright © 2015年 qianfeng. All rights reserved.
//

#import "StarView.h"

@implementation StarView
{
    UIImageView * _bgImageView;
    UIImageView * _frImageView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _bgImageView.image = [UIImage imageNamed:@"StarsBackground"];
        [self addSubview:_bgImageView];
        
        _frImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _frImageView.image = [UIImage imageNamed:@"StarsForeground"];
        [self addSubview:_frImageView];
        // 图片居左停靠 父视图变化时 图像始终居左停靠
        // 这个属性如果不开 那么self变小时 frImageView等比例变小 失去效果
        _frImageView.contentMode = UIViewContentModeLeft;
        // 超出视图部分切除
        _frImageView.clipsToBounds = YES;
    }
    return self;
}

// 重写rating的setter方法 修改ImageView的frame
- (void)setRating:(CGFloat)rating
{
    _rating = rating;
    CGFloat width = rating / 5 * _bgImageView.frame.size.width;
    _frImageView.frame = CGRectMake(0, 0, width, _bgImageView.frame.size.height);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
